package com.ruoyi.web.mnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mnt.domain.bo.MntHistoryBo;
import com.ruoyi.web.mnt.domain.vo.MntHistoryVo;
import com.ruoyi.web.mnt.domain.MntHistory;
import com.ruoyi.web.mnt.mapper.MntHistoryMapper;
import com.ruoyi.web.mnt.service.IMntHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设施巡维记录Service业务层处理
 *
 * @author LIUJUN
 * @date 2022-06-24
 */
@RequiredArgsConstructor
@Service
public class MntHistoryServiceImpl implements IMntHistoryService {

    private final MntHistoryMapper baseMapper;

    /**
     * 查询设施巡维记录
     *
     * @param id 设施巡维记录主键
     * @return 设施巡维记录
     */
    @Override
    public MntHistoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设施巡维记录列表
     *
     * @param bo 设施巡维记录
     * @return 设施巡维记录
     */
    @Override
    public TableDataInfo<MntHistoryVo> queryPageList(MntHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MntHistory> lqw = buildQueryWrapper(bo);
        Page<MntHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设施巡维记录列表
     *
     * @param bo 设施巡维记录
     * @return 设施巡维记录
     */
    @Override
    public List<MntHistoryVo> queryList(MntHistoryBo bo) {
        LambdaQueryWrapper<MntHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MntHistory> buildQueryWrapper(MntHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MntHistory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFacilityName()), MntHistory::getFacilityName, bo.getFacilityName());
        lqw.like(StringUtils.isNotBlank(bo.getMgrName()), MntHistory::getMgrName, bo.getMgrName());
        lqw.eq(bo.getDutyDate() != null, MntHistory::getDutyDate, bo.getDutyDate());
        return lqw;
    }

    /**
     * 新增设施巡维记录
     *
     * @param bo 设施巡维记录
     * @return 结果
     */
    @Override
    public Boolean insertByBo(MntHistoryBo bo) {
        MntHistory add = BeanUtil.toBean(bo, MntHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设施巡维记录
     *
     * @param bo 设施巡维记录
     * @return 结果
     */
    @Override
    public Boolean updateByBo(MntHistoryBo bo) {
        MntHistory update = BeanUtil.toBean(bo, MntHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MntHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设施巡维记录
     *
     * @param ids 需要删除的设施巡维记录主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
